﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

using Infrastructure.NetExtensions;
using TempEmployees.DAL;
using TempEmployees.BLL.Database;
using TempEmployees.BLL.EntitiesExtensions;

namespace TempEmployees.Web
{
    public partial class GridViewAssignments : BaseGridView
    {
        public delegate void dGotoAssignmentDetailes(int assignmentId);
       
        private static readonly string SHOW_NOT_ACTIVE = "show_not_active";
        private static readonly string FILTERS = "filters";

        private Filters Filters
        {
            get
            {
                return ViewState.Get<Filters>(FILTERS);
            }
            set
            {
                ViewState.Set<Filters>(FILTERS, value);
            }
        }
        
        private bool ShowNotActive // show not active assignments in the GridView
        {
            get
            {
                return ViewState.Get<bool>(SHOW_NOT_ACTIVE);
            }
            set
            {
                ViewState.Set<bool>(SHOW_NOT_ACTIVE, value);
            }
        }

        private const int COLUMN_ICON = 0;
        private const int COLUMN_SELECT_BUTTON = 1;
        private const int COLUMN_FORM_BUTTON = 2;
        private const int COLUMN_ICON_URL = 3;
        private const int COLUMN_ASSIGNMENT_ID = 4;
        private const int COLUMN_TOOL_TIP = 5;
        private const int COLUMN_STATUS_ID = 10;

        public dGotoAssignmentDetailes GotoAssignmentDetailes { get; set; }

        public void SetGridView(Filters filters, bool allowCheckBox)
        {
            if (filters != null)
            {
                Filters = new Filters(filters);
            }
            else
            {
                filters = new Filters(Filters);
            }
            
            if (allowCheckBox && !CheckBoxShowNotActive.Checked)
            {
                List<object> statusAllowed = new List<object>();
                
                statusAllowed.Add((int)DbConstants.Status.Active);
                statusAllowed.Add((int)DbConstants.Status.Approved);
                statusAllowed.Add((int)DbConstants.Status.Pending);
                statusAllowed.Add((int)DbConstants.Status.Rejected);
                
                filters.Add("StatusID", statusAllowed);
            }

            DataTable dataTable = new DataTable();
            GridViewDataTable = dataTable;

            LabelEmptyQueryAssignments.Visible = !DbServices.FillAssignmentsGridView(dataTable, filters);
            CheckBoxShowNotActive.Visible = allowCheckBox;

            base.SetGridView();
        }

        public void DisableGridViewCells(SessionState state)
        {
            int assignmentId;
            foreach (GridViewRow row in ViewAssignments.Rows)
            {
                if (state.UserRole == DbConstants.Roles.Employer && int.TryParse(row.Cells[COLUMN_ASSIGNMENT_ID].Text, out assignmentId))
                {
                    Assignment assignment = EAssignment.GetAssignmentById(assignmentId);
                    if (assignment.UserID != state.UserId)
                    {
                        row.Cells[COLUMN_SELECT_BUTTON].Enabled = false;
                        row.Cells[COLUMN_SELECT_BUTTON].Text = WebConfigData.GRIDVIEW_CELL_BLOCKED;
                        row.Cells[COLUMN_FORM_BUTTON].Enabled = false;
                        row.Cells[COLUMN_FORM_BUTTON].Text = WebConfigData.GRIDVIEW_CELL_BLOCKED;
                    }
                }
            }
        }

        private void SetFormButton(GridViewRow row)
        {
            LinkButton formButton = (LinkButton)row.Cells[COLUMN_FORM_BUTTON].Controls[0];
            DbConstants.Status status = GetRowStatus(row);
            switch (status)
            {
                case DbConstants.Status.Active:
                case DbConstants.Status.Finished:
                    formButton.Text = WebConfigData.FORM_VIEW;
                    break;
                case DbConstants.Status.Approved:
                    formButton.Text = WebConfigData.FORM_CREATE;
                    break;
                case DbConstants.Status.Rejected:
                case DbConstants.Status.Pending:
                    formButton.Enabled = false;
                    formButton.Text = string.Empty;
                    break;
            }
        }

        private DbConstants.Status GetRowStatus(GridViewRow row)
        {
            return (DbConstants.Status) int.Parse(row.Cells[COLUMN_STATUS_ID].Text);
        }
             
        protected void GridViewAssignments_SelectedIndexChanged(object sender, EventArgs e)
        {
            int rowIndex = ViewAssignments.SelectedIndex;
            GridViewRow row = ViewAssignments.Rows[rowIndex];
            string assignmentId = row.Cells[COLUMN_ASSIGNMENT_ID].Text;
            GotoAssignmentDetailes(int.Parse(assignmentId));
        }

        protected void GridViewAssignments_RowCreated(object sender, GridViewRowEventArgs e)
        {
            //hide the AssignmentID & ICON_URL columns
            if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[COLUMN_ICON_URL].CssClass = "hiddencol";
                e.Row.Cells[COLUMN_TOOL_TIP].CssClass = "hiddencol";
                e.Row.Cells[COLUMN_STATUS_ID].CssClass = "hiddencol";
            }
        }

        protected void GridViewAssignments_FormRequest(object sender, GridViewEditEventArgs e)
        {
            int index = e.NewEditIndex;
            int assignmentId = int.Parse(ViewAssignments.Rows[index].Cells[COLUMN_ASSIGNMENT_ID].Text);
            Assignment assignment = EAssignment.GetAssignmentById(assignmentId);
            assignment.ProduceForm();
        }

        protected void CheckBoxShowNotActive_CheckedChanged(object sender, EventArgs e)
        {
            SetGridView(null,true);
        }

        protected void ViewAssignments_DataBound(object sender, EventArgs e)
        {
            foreach (GridViewRow row in ViewAssignments.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    //tooltip
                    row.Cells[COLUMN_ICON].Attributes.Add("Title", row.Cells[COLUMN_TOOL_TIP].Text);
                    //form button
                    SetFormButton(row);
                }
            }
        }

        protected override GridView GetGridView()
        {
            return ViewAssignments;
        }

        protected override ObjectDataSource GetDataSource()
        {
            return AssignmentsDataSource;
        }
    }
}